package com.example.testdemo.kinetic.sprite;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.manager.ResourcesManager;
import com.example.testdemo.manager.ScenesManager;
import com.example.testdemo.resources.CharacterResources;
import com.example.testdemo.scenes.stage.BaseStage;

public abstract class KineticSprite extends AnimatedSprite {

	public static interface IStateChangeListener {
		public void onStateChange();
	}

	public String NAME = null;

	protected TankGameActivity mActivity = null;
	protected CharacterResources mCharacterResources = null;
	private PhysicsHandler mPhysicsHandler = null;
	protected int mWhichScene = 0;
	protected BaseStage mOnScene = null;

	public KineticSprite(int whichScene, String pName, float pX, float pY,
	        ITiledTextureRegion pTiledTextureRegion, TankGameActivity pActivity) {
		super(pX, pY, pTiledTextureRegion, pActivity
		        .getVertexBufferObjectManager());
		NAME = pName;
		initFields(pActivity, whichScene);
		setCullingEnabled(true);
	}

	private void initFields(TankGameActivity pActivity, int whichScene) {
		mWhichScene = whichScene;
		mOnScene = (BaseStage) ScenesManager.getInstance().getScene(
		        whichScene);

		mActivity = pActivity;
		mCharacterResources = (CharacterResources) ResourcesManager
		        .getInstance().getResources(
		                ResourcesManager.RESOURCES_CHARACTER);
	}

	public void setPhysicsHandler(PhysicsHandler handler) {
		if (handler != null && mPhysicsHandler == null) {
			mPhysicsHandler = handler;
			registerUpdateHandler(mPhysicsHandler);

		} else if (handler == null && mPhysicsHandler != null) {
			unregisterUpdateHandler(mPhysicsHandler);
		}
	}

	@Override
	public void setVisible(boolean pVisible) {
		super.setVisible(pVisible);
		if (pVisible) {
			resetSpriteData();
		}
	}

	public void setVelocity(float pVelocityX, float pVelocityY) {
		if (mPhysicsHandler != null) {
			mPhysicsHandler.setVelocity(pVelocityX, pVelocityY);
		}
	}

	protected abstract void resetSpriteData();

	public abstract void free();
}
